<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\User;
use app\api\common\Program;
use think\Config;
use think\Db;
use think\Validate;

use app\common\model\Typeone;
use app\common\model\Typetwo;
use app\common\model\Goods;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页轮播图
     *
     */
    public function banner()
    {
        $data = Db::name("banner")->field("id, image, jump")->where("status", "=", "1")->order("weigh desc")->select();
        // 循环查询结果
        foreach ($data as $key => &$value) {
            // 为image拼接域名
            $value["image"] = cdnurl($value["image"], true);
        }
        $this->success('请求成功', $data);
    }
    /**
     * 轮播图富文本
     */
    public function getBannerRich()
    {
        $id = $this->request->param('id/d', 0);
        if (!$id) {
            $this->error('未填必传参数');
        }
        $res = Db::name('banner')->where('id', $id)->find();
        if ($res['jump'] == '2') {
            $this->error('该轮播图不支持跳转');
        }
        // $rich = $res['content'];
        $rich = getHandleRichtext($res['content']);
        $this->success('请求成功', $rich);
    }
    /**
     * 首页分类/一级分类
     *
     */
    public function getTypeOne()
    {
        // 接收参数
        $type = $this->request->post("type/d", 1);

        if ($type == 1) {
            $data = Typeone::field("id, name, image")->where("status", "=", "1")->order("weigh desc")->limit(10)->select();
        } else {
            $data = Typeone::field("id, name, image")->where("status", "=", "1")->order("weigh desc")->select();
        }
        $this->success('请求成功', $data);
    }

    /**
     * 热销分类/二级分类
     *
     */
    public function getTypeTwo()
    {
        // 接收参数
        $type = $this->request->post("type/d", 1);

        if ($type == 1) {  // 热销分类
            $data = Typetwo::field("id, p_id, name, image, recommend")->where("typetwo.recommend", "1")->where("typetwo.status", "1")->with(['typeone'])->order("typetwo.weigh desc")->select();
        } else {  // 二级分类
            // 接收参数
            $one_id = $this->request->post("one_id/d", 0);

            if (empty($one_id)) {
                $this->error('数据不存在');
            }

            $data = Typetwo::field("id, p_id, name, image, recommend")->where("typetwo.p_id", $one_id)->where("typetwo.status", "1")->with(['typeone'])->order("typetwo.weigh desc")->select();
        }
        foreach ($data as $row) {

            $row->getRelation('typeone')->visible(['name']);
        }
        $this->success('请求成功', $data);
    }

    /**
     * 热销商品
     *
     */
    public function hotGoods()
    {
        // 接收参数
        $two_id = $this->request->post("two_id/d", 0);

        if (empty($two_id)) {
            $this->error('数据不存在');
        }
        $where = [
            "two_id" => $two_id,
            "recommend" => "1",
            "status" => "1",
        ];

        $data = Goods::field("id, name, spec, blurb,goods_image,goods_images")->where($where)->order("weigh desc")->select();
        // foreach ($data as $key => $item) {
        //     $data[$key]['']
        // }
        $this->success('请求成功', $data);
    }

    /**
     * 获取用户信息
     */
    public function UserInfo()
    {
        $user_id = $this->auth->id;
        if (empty($user_id)) {
            $this->error('用户不存在');
        }
        // $info = User::getUserInfo($user_id);
        $info = User::find($user_id);
        $info['avatar'] = cdnurl($info['avatar'], true);
        unset($info['password']);
        unset($info['salt']);
        unset($info['openid']);
        $this->success('请求成功', $info);
    }
    /**
     * 修改用户信息
     */
    public function userprofile()
    {
        $user = $this->auth->getUser();
        $nickname = $this->request->post('nickname');
        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $avatar = parse_url($avatar)['path'];
        if ($nickname) {
            $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('用户名已存在'));
            }
            $user->nickname = $nickname;
        }
        $user->avatar = $avatar;
        $user->save();
        $this->success();
    }
}
